package com.example.pcl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pcl.constant.MessageConstants;
import com.example.pcl.exception.ObjectException;
import com.example.pcl.mapper.AppointmentsMapper;
import com.example.pcl.pojo.dto.ConsultationRecordDTO;
import com.example.pcl.pojo.dto.ConsultationRecordQueryDTO;
import com.example.pcl.pojo.entity.ConsultationRecords;
import com.example.pcl.mapper.ConsultationRecordsMapper;
import com.example.pcl.pojo.vo.ConsultationRecordVO;
import com.example.pcl.result.PageResult;
import com.example.pcl.service.IConsultationRecordsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 咨询记录表，记录每次咨询的详细情况 服务实现类
 * </p>
 *
 * @author xx
 * @since 2024-12-17
 */
@Service
@RequiredArgsConstructor
public class ConsultationRecordsServiceImpl extends ServiceImpl<ConsultationRecordsMapper, ConsultationRecords> implements IConsultationRecordsService {

    private final AppointmentsMapper appointmentsMapper;

    @Override
    public PageResult<ConsultationRecordVO> queryConsultationRecords(@NotNull ConsultationRecordQueryDTO queryDTO) {
        IPage<ConsultationRecordVO> iPage = baseMapper.pageList(new Page<ConsultationRecords>(queryDTO.getPageNum(), queryDTO.getPageSize()), queryDTO);
        return new PageResult<>(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addConsultationRecord(ConsultationRecordDTO recordDTO) {
        ConsultationRecords record = new ConsultationRecords();
        BeanUtils.copyProperties(recordDTO, record);
        int rows = baseMapper.insert(record);
        if (rows <= 0) {
            throw new ObjectException(MessageConstants.ADD_ERR);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateConsultationRecord(ConsultationRecordDTO recordDTO) {
        ConsultationRecords record = new ConsultationRecords();
        BeanUtils.copyProperties(recordDTO, record);
        int rows = baseMapper.updateById(record);
        if (rows <= 0) {
            throw new ObjectException(MessageConstants.UPDATE_ERR);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteConsultationRecord(Integer recordId) {
        ConsultationRecords records = getById(recordId);
        if (records == null) {
            throw new ObjectException("咨询记录不存在");
        }
        int rows = baseMapper.deleteById(recordId);
        if (rows <= 0) {
            throw new ObjectException(MessageConstants.DELETE_ERR);
        }
        int result = appointmentsMapper.deleteById(records.getAppointmentId());
        if (result < 0) {
            throw new ObjectException(MessageConstants.DELETE_ERR);
        }

    }

    @Override
    public ConsultationRecords finByAppointmentId(Integer id) {
        return getOne(new LambdaQueryWrapper<ConsultationRecords>().eq(ConsultationRecords::getAppointmentId, id));
    }


}
